package com.block.channel.service.monitor;

import cn.hutool.core.util.ObjectUtil;
import com.alibaba.fastjson.JSON;
import com.block.channel.enums.MonitorChannelEnum;
import com.block.channel.model.dto.MonitorInfoDTO;
import com.block.channel.service.IChannelMonitorService;
import com.block.db.entity.ChannelBaseInfo;
import org.springframework.stereotype.Service;

import java.util.Map;


/**
 * https://api.koukoudai.shop/apinotice/channel/notifyResult/huawei/huawei?aid=__AID__&aidName=__AID_NAME__&oaid=__OAID__&idType=__ID_TYPE__&uniqueId=__UNIQUE_ID__&actionType=__ACTION_TYPE__&callback=__CALLBACK__
 */
@Service
public class HuaWeiMonitorService implements IChannelMonitorService {

    @Override
    public MonitorInfoDTO handleRequestParam(ChannelBaseInfo channelBaseInfo, String actionType, Map<String, String[]> parameterMap, String paramContent) {

        MonitorInfoDTO monitorInfoDTO = new MonitorInfoDTO();

        monitorInfoDTO.setPlatform(MonitorChannelEnum.HUAWEI.getValue());
        monitorInfoDTO.setChannelCode(channelBaseInfo.getChannelCode());
        monitorInfoDTO.setChannelName(channelBaseInfo.getChannelName());
        monitorInfoDTO.setMonitorInfo(JSON.toJSONString(parameterMap));

        String[] oaidArr = parameterMap.get("oaid");
        String[] idTypeArr = parameterMap.get("idType");
        String[] uniqueIdArr = parameterMap.get("uniqueId");
        String[] callbackArr = parameterMap.get("callback");
        String[] actionTypeArr = parameterMap.get("actionType");

        if (ObjectUtil.isNotEmpty(oaidArr)){
            monitorInfoDTO.setUniqueKey(oaidArr[0]);
            monitorInfoDTO.setKeyType("OAID");
        }

//        if (ObjectUtil.isNotEmpty(idTypeArr)){
//            monitorInfoDTO.setKeyType(idTypeArr[0]);
//        }
//        if (ObjectUtil.isNotEmpty(uniqueIdArr)){
//            monitorInfoDTO.setUniqueKey(uniqueIdArr[0]);
//        }
        if (ObjectUtil.isNotEmpty(callbackArr)){
            monitorInfoDTO.setCallbackInfo(callbackArr[0]);
        }
        if (ObjectUtil.isNotEmpty(actionTypeArr)){
            monitorInfoDTO.setActionType(actionTypeArr[0]);
        }

        return monitorInfoDTO;
    }
}
